import { BaseManager } from '../../libs/rxjs/cc3/BaseManager';
import { _decorator, color, director, System } from 'cc';
import { sys } from 'cc';
import { jsb_sdk_m } from './JsbSDKManager';
import { udm } from '../UserDataManager';
import { thinking_sdk_m } from './ThinkingCocosSDKManager';
import { ReactiveProperty } from '../../libs/rxjs/cc3/ReactiveProperty';
import { timem } from '../TimeManager';
import { DevConfig } from '../DevConfig';
const { ccclass, property } = _decorator;

@ccclass('AppsFlyerSDKManager')
export class AppsFlyerSDKManager extends BaseManager {

    public static instance: AppsFlyerSDKManager;
    static ID = 'appsflyer_sdk_system';
    get logColorOverride() { return color().fromHEX('#EE5533') };

    conversionData = ReactiveProperty.Create<Record<string, any>>(null);

    static registerSystem() {
        director.unregisterSystem(director.getSystem(AppsFlyerSDKManager.ID));
        director.registerSystem(AppsFlyerSDKManager.ID, appsflyer_sdk_m, System.Priority.MEDIUM);
        appsflyer_sdk_m.init();
    }

    onInitManager() {
        this.log('onInitManager', this.timestamp);
        if (sys.isNative) {
            jsb_sdk_m.registerMsg('AppsFlyerSDKManager.responseConversionData', this.responseConversionData.bind(this));
        }
        this.logEvent('af_app_opened');
    }

    useObserves() {
        return [
            this.conversionData.subscribe(_ => {
                if (this.conversionData.value != null) {
                    if (this.conversionData.value?.['af_status'] == 'Organic') {
                        thinking_sdk_m.requestUserSet({
                            Latest_Source: this.conversionData.value?.['af_status'] // Organic
                        });
                    } else {
                        thinking_sdk_m.requestUserSet({
                            Latest_Source: this.conversionData.value?.['af_channel']
                        });
                    }
                }
            })
        ];
    }

    responseConversionData(args: string) {
        this.log('@responseConversionData', args);
        this.conversionData.value = JSON.parse(args); // af_channel, af_status
    }

    logEvent(eventName: string) {
        this.log('logEvent', eventName);
        if (!DevConfig.DATA_REPORT) return;
        jsb_sdk_m.sendMsg('AppsFlyerCocosSDK.logEvent', JSON.stringify({ eventName }));
    }

    AdRewardAD_Xs = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 40, 80]);
    InsertionAD_Xs = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30, 35, 40, 50, 80, 100, 150]);
    Level_Xs = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 130, 150, 180, 200, 250, 300]);

    // 检查首次激活后24h内在线时长30min
    check_af_r30r() {
        if (!udm.af_r30r_logged.value) {
            let launchTimeTotal = udm.launchTimeTotal.value + timem.now() - jsb_sdk_m.startAt;
            // this.log('@check_af_r30r', launchTimeTotal);
            if (launchTimeTotal > 1800 * 1000) {
                this.logEvent('af_r30r');
                udm.af_r30r_logged.value = true;
                udm.save();
            }
        }
    }
}
globalThis.AppsFlyerSDKManager = AppsFlyerSDKManager;

export const appsflyer_sdk_m = AppsFlyerSDKManager.instance = new AppsFlyerSDKManager();